/* *****************************************************************************************************
 * Copyright 2009. Dbwebx Group
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 * *****************************************************************************************************
 */

package org.dbwebx.persistence;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

import java.io.Serializable;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;


/**
 * The entity represents person who can perform some activities.
 *
 * @author Izzet_Mustafayev
 *
 */
@Entity
@Table(name = "t_users")
@NamedQueries({@NamedQuery(name = User.FIND_BY_LOGIN,query = "FROM User u WHERE u.login=:login")
})
public class User implements Serializable {
    public static final String FIND_BY_LOGIN = "findUserByLogin";

    /**
     * serialVersionUID.
     */
    private static final long serialVersionUID = 1239489283794270L;

    /**
     * {@link User}'s identity.
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long identity;

    /**
     * {@link User}'s login name.
     */
    private String login;

    /**
     * {@link User}'s first name.
     */
    @Column(name = "first_name")
    private String firstName;

    /**
     * {@link User}'s last name.
     */
    @Column(name = "last_name")
    private String lastName;

    /**
     * The source node last modifier user.
     */
    @ManyToOne
    @JoinColumn(name = "modifier_id")
    private User modifier;

    /**
     * The source node modification time.
     */
    @Column(name = "updated_on")
    @Temporal(TemporalType.TIMESTAMP)
    private Date updatedOn;

    /**
     * @return the identity
     */
    public Long getIdentity() {
        return identity;
    }

    /**
     * @param identity
     *            the identity to set
     */
    public void setIdentity(final Long identity) {
        this.identity = identity;
    }

    /**
     * @return the login
     */
    public String getLogin() {
        return login;
    }

    /**
     * @param login
     *            the login to set
     */
    public void setLogin(final String login) {
        this.login = login;
    }

    /**
     * @return the firstName
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * @param firstName
     *            the firstName to set
     */
    public void setFirstName(final String firstName) {
        this.firstName = firstName;
    }

    /**
     * @return the lastName
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * @param lastName
     *            the lastName to set
     */
    public void setLastName(final String lastName) {
        this.lastName = lastName;
    }

    /**
     * @return the modifier
     */
    public User getModifier() {
        return modifier;
    }

    /**
     * @param modifier
     *            the modifier to set
     */
    public void setModifier(final User modifier) {
        this.modifier = modifier;
    }

    /**
     * @return the updatedOn
     */
    public Date getUpdatedOn() {
        return updatedOn;
    }

    /**
     * @param updatedOn
     *            the updatedOn to set
     */
    public void setUpdatedOn(final Date updatedOn) {
        this.updatedOn = updatedOn;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this).append(identity).append(login).toString();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof User == false) {
            return false;
        }

        if (this == obj) {
            return true;
        }

        User rhs = (User) obj;

        return new EqualsBuilder().append(identity, rhs.identity).append(login, rhs.login).isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(11, 49).append(identity).append(login).toHashCode();
    }
}
